<template>
  <div class="home-container">
    <!-- 页面标题 -->
    <div class="page-header">
      <h1 class="page-title">人资管理系统</h1>
      <p class="page-subtitle">欢迎回来，管理员</p>
    </div>

    <!-- 数据概览卡片 -->
    <div class="stats-grid">
      <div class="stat-card">
        <div class="stat-icon orders">
          <el-icon><ShoppingCart /></el-icon>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ stats.totalEmployees }}</div>
          <div class="stat-label">在职员工</div>
          <div class="stat-change positive">+12.5%</div>
        </div>
      </div>

      <div class="stat-card">
        <div class="stat-icon revenue">
          <el-icon><Money /></el-icon>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ stats.openPositions }}</div>
          <div class="stat-label">开放岗位</div>
          <div class="stat-change positive">+8.3%</div>
        </div>
      </div>

      <div class="stat-card">
        <div class="stat-icon products">
          <el-icon><Goods /></el-icon>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ stats.monthHires }}</div>
          <div class="stat-label">本月入职</div>
          <div class="stat-change positive">+5.2%</div>
        </div>
      </div>

      <div class="stat-card">
        <div class="stat-icon users">
          <el-icon><User /></el-icon>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ stats.monthLeaves }}</div>
          <div class="stat-label">本月离职</div>
          <div class="stat-change positive">+15.7%</div>
        </div>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="main-content">
      <!-- 左侧：图表区域 -->
      <div class="charts-section">
        <!-- 人力成本趋势 -->
        <div class="chart-card span-2">
          <div class="chart-header">
            <h3>人力成本趋势</h3>
            <el-select v-model="chartPeriod" size="small" style="width: 120px">
              <el-option label="7天" value="7" />
              <el-option label="30天" value="30" />
              <el-option label="90天" value="90" />
            </el-select>
          </div>
          <div ref="salesChartRef" class="chart-container"></div>
        </div>

        <!-- 部门人员占比 -->
        <div class="chart-card">
          <div class="chart-header">
            <h3>部门人员占比</h3>
          </div>
          <div ref="categoryChartRef" class="chart-container"></div>
        </div>

        <!-- 性别占比 -->
        <div class="chart-card">
          <div class="chart-header">
            <h3>性别占比</h3>
          </div>
          <div ref="genderChartRef" class="chart-container"></div>
        </div>

        <!-- 部门人数Top5 -->
        <div class="chart-card">
          <div class="chart-header">
            <h3>部门人数Top5</h3>
          </div>
          <div ref="deptChartRef" class="chart-container"></div>
        </div>
      </div>

      <!-- 右侧：快捷操作和状态监控 -->
      <div class="sidebar-section">
        <!-- 快捷操作 -->
        <div class="quick-actions">
          <h3>快捷操作</h3>
          <div class="action-buttons">
            <el-button type="primary" @click="handleAddEmployee">
              <el-icon><Plus /></el-icon>
              新增员工
            </el-button>
            <el-button type="success" @click="handleRecruitment">
              <el-icon><List /></el-icon>
              招聘管理
            </el-button>
            <el-button type="warning" @click="handleAttendance">
              <el-icon><User /></el-icon>
              考勤管理
            </el-button>
            <el-button type="info" @click="handlePayroll">
              <el-icon><TrendCharts /></el-icon>
              薪酬结算
            </el-button>
          </div>
        </div>

        <!-- 订单状态监控 -->
        <div class="order-status">
          <h3>员工状态</h3>
          <div class="status-list">
            <div class="status-item">
              <span class="status-dot pending"></span>
              <span class="status-text">试用期</span>
              <span class="status-count">{{ employeeStatus.probation }}</span>
            </div>
            <div class="status-item">
              <span class="status-dot processing"></span>
              <span class="status-text">正式</span>
              <span class="status-count">{{ employeeStatus.fulltime }}</span>
            </div>
            <div class="status-item">
              <span class="status-dot shipped"></span>
              <span class="status-text">请假中</span>
              <span class="status-count">{{ employeeStatus.onleave }}</span>
            </div>
            <div class="status-item">
              <span class="status-dot completed"></span>
              <span class="status-text">离职</span>
              <span class="status-count">{{ employeeStatus.left }}</span>
            </div>
          </div>
        </div>

        <!-- 人事动态 -->
        <div class="recent-activities">
          <h3>人事动态</h3>
          <div class="activity-list">
            <div v-for="activity in recentActivities" :key="activity.id" class="activity-item">
              <div class="activity-icon">
                <el-icon><Bell /></el-icon>
              </div>
              <div class="activity-content">
                <div class="activity-text">{{ activity.text }}</div>
                <div class="activity-time">{{ activity.time }}</div>
              </div>
            </div>
          </div>
        </div>

        <!-- 待办事项 -->
        <div class="todo-section">
          <h3>待办事项</h3>
          <div class="todo-list">
            <label v-for="item in todos" :key="item.id" class="todo-item">
              <input type="checkbox" v-model="item.done" />
              <span :class="['todo-text', { done: item.done }]">{{ item.text }}</span>
              <span class="todo-time">{{ item.time }}</span>
            </label>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, nextTick } from 'vue'
import { init, use } from 'echarts/core'
import { LineChart, PieChart, BarChart } from 'echarts/charts'
import {
  TitleComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent
} from 'echarts/components'
import { CanvasRenderer } from 'echarts/renderers'
import { ElMessage } from 'element-plus'
import { ShoppingCart,Plus,Money,Goods,User,List,TrendCharts,Bell } from '@element-plus/icons-vue'

// 注册ECharts组件
use([
  TitleComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
  LineChart,
  PieChart,
  BarChart,
  CanvasRenderer
])
// 响应式数据
const chartPeriod = ref('7')
const salesChartRef = ref<HTMLElement>()
const categoryChartRef = ref<HTMLElement>()
const genderChartRef = ref<HTMLElement>()
const deptChartRef = ref<HTMLElement>()

// 统计数据（人资）
const stats = ref({
  totalEmployees: 865,
  openPositions: 34,
  monthHires: 18,
  monthLeaves: 6
})

// 员工状态
const employeeStatus = ref({
  probation: 42,
  fulltime: 768,
  onleave: 19,
  left: 36
})

// 人事动态
const recentActivities = ref([
  { id: 1, text: '王小明 完成人事入职手续（研发部）', time: '2分钟前' },
  { id: 2, text: '发布新岗位：前端开发工程师（3人）', time: '25分钟前' },
  { id: 3, text: '李四 申请年假 5 天', time: '1小时前' },
  { id: 4, text: '本月薪资已完成发放', time: '昨天 18:20' },
  { id: 5, text: '安全培训完成，覆盖率 98%', time: '前天 10:10' }
])

// 待办事项
const todos = ref([
  { id: 1, text: '审批：张三转正申请', time: '今天 17:30', done: false },
  { id: 2, text: '面试：后端开发一面 ×2', time: '明天 10:00', done: false },
  { id: 3, text: '发布：校园招聘计划', time: '本周内', done: false },
  { id: 4, text: '提醒：提交下月排班', time: '本周五', done: false }
])

// 格式化数字
const formatNumber = (num: number): string => {
  return num.toLocaleString('zh-CN')
}

// 初始化人力成本趋势图
const initSalesChart = () => {
  if (!salesChartRef.value) return
  
  const chart = init(salesChartRef.value)
  const option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross'
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
      axisLine: {
        lineStyle: {
          color: '#e0e0e0'
        }
      }
    },
    yAxis: {
      type: 'value',
      axisLine: {
        lineStyle: {
          color: '#e0e0e0'
        }
      },
      splitLine: {
        lineStyle: {
          color: '#f5f5f5'
        }
      }
    },
    series: [
      {
        name: '人力成本',
        type: 'line',
        smooth: true,
        data: [120, 128, 132, 138, 145, 149, 155],
        itemStyle: {
          color: '#409eff'
        },
        areaStyle: {
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [
              { offset: 0, color: 'rgba(64, 158, 255, 0.3)' },
              { offset: 1, color: 'rgba(64, 158, 255, 0.1)' }
            ]
          }
        }
      }
    ]
  }
  chart.setOption(option)
}

// 初始化部门人员占比图
const initCategoryChart = () => {
  if (!categoryChartRef.value) return
  
  const chart = init(categoryChartRef.value)
  const option = {
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b}: {c} ({d}%)'
    },
    legend: {
      orient: 'vertical',
      left: 'left',
      textStyle: {
        fontSize: 12
      }
    },
    series: [
      {
        name: '部门人员',
        type: 'pie',
        radius: ['40%', '70%'],
        data: [
          { value: 320, name: '研发部' },
          { value: 160, name: '产品部' },
          { value: 120, name: '设计部' },
          { value: 180, name: '销售部' },
          { value: 85, name: '人资行政' }
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  }
  chart.setOption(option)
}

// 初始化性别占比（环形图）
const initGenderChart = () => {
  if (!genderChartRef.value) return
  const chart = init(genderChartRef.value)
  const option = {
    tooltip: { trigger: 'item' },
    legend: { bottom: 0 },
    series: [
      {
        name: '性别占比',
        type: 'pie',
        radius: ['50%', '70%'],
        avoidLabelOverlap: false,
        label: { show: true, formatter: '{b}: {d}%' },
        data: [
          { value: 520, name: '男性' },
          { value: 345, name: '女性' }
        ]
      }
    ]
  }
  chart.setOption(option)
}

// 初始化部门人数Top5（柱状图）
const initDeptChart = () => {
  if (!deptChartRef.value) return
  const chart = init(deptChartRef.value)
  const option = {
    tooltip: { trigger: 'axis' },
    grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true },
    xAxis: {
      type: 'category',
      data: ['研发部', '销售部', '产品部', '设计部', '客服中心']
    },
    yAxis: { type: 'value' },
    series: [
      {
        name: '人数',
        type: 'bar',
        data: [320, 180, 160, 120, 90],
        itemStyle: { color: '#67C23A' },
        barWidth: 24,
        roundCap: true
      }
    ]
  }
  chart.setOption(option)
}

// 快捷操作处理函数
const handleAddEmployee = () => {
  ElMessage.success('跳转到新增员工页面')
}

const handleRecruitment = () => {
  ElMessage.success('跳转到招聘管理页面')
}

const handleAttendance = () => {
  ElMessage.success('跳转到考勤管理页面')
}

const handlePayroll = () => {
  ElMessage.success('跳转到薪酬结算页面')
}

// 生命周期
onMounted(async () => {
  await nextTick()
  initSalesChart()
  initCategoryChart()
  initGenderChart()
  initDeptChart()
})
</script>

<style scoped>
.home-container {
  padding: 24px;
  background-color: #f5f7fa;
  min-height: 100vh;
}

.page-header {
  margin-bottom: 32px;
}

.page-title {
  font-size: 28px;
  font-weight: 600;
  color: #303133;
  margin: 0 0 8px 0;
}

.page-subtitle {
  font-size: 16px;
  color: #909399;
  margin: 0;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 24px;
  margin-bottom: 32px;
}

.stat-card {
  background: white;
  border-radius: 12px;
  padding: 24px;
  display: flex;
  align-items: center;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 20px;
  font-size: 24px;
  color: white;
}

.stat-icon.orders {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.stat-icon.revenue {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.stat-icon.products {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.stat-icon.users {
  background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
}

.stat-content {
  flex: 1;
}

.stat-number {
  font-size: 28px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 14px;
  color: #909399;
  margin-bottom: 8px;
}

.stat-change {
  font-size: 12px;
  font-weight: 500;
}

.stat-change.positive {
  color: #67c23a;
}

.main-content {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 24px;
}

.charts-section {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 24px;
}

.chart-card {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.chart-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #303133;
}

.chart-container {
  height: 300px;
  width: 100%;
}

/* 图表区跨两列的卡片 */
.span-2 {
  grid-column: span 2;
}

.sidebar-section {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.quick-actions,
.order-status,
.recent-activities,
.todo-section {
  background: white;
  border-radius: 12px;
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

.quick-actions h3,
.order-status h3,
.recent-activities h3,
.todo-section h3 {
  margin: 0 0 20px 0;
  font-size: 18px;
  font-weight: 600;
  color: #303133;
}

/* 待办样式 */
.todo-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.todo-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border: 1px solid #f0f0f0;
  border-radius: 8px;
}

.todo-text {
  flex: 1;
  margin-left: 10px;
}

.todo-text.done {
  color: #909399;
  text-decoration: line-through;
}

.todo-time {
  color: #909399;
  font-size: 12px;
  margin-left: 12px;
}

.action-buttons {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.action-buttons .el-button {
  justify-content: flex-start;
  height: 44px;
}

.status-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.status-item {
  display: flex;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
}

.status-item:last-child {
  border-bottom: none;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 12px;
}

.status-dot.pending {
  background-color: #e6a23c;
}

.status-dot.processing {
  background-color: #409eff;
}

.status-dot.shipped {
  background-color: #67c23a;
}

.status-dot.completed {
  background-color: #909399;
}

.status-text {
  flex: 1;
  font-size: 14px;
  color: #606266;
}

.status-count {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
}

.activity-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.activity-item {
  display: flex;
  align-items: flex-start;
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
}

.activity-item:last-child {
  border-bottom: none;
}

.activity-icon {
  width: 32px;
  height: 32px;
  background-color: #f0f9ff;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
  color: #409eff;
}

.activity-content {
  flex: 1;
}

.activity-text {
  font-size: 14px;
  color: #303133;
  margin-bottom: 4px;
  line-height: 1.4;
}

.activity-time {
  font-size: 12px;
  color: #909399;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .main-content {
    grid-template-columns: 1fr;
  }
  
  .sidebar-section {
    order: -1;
  }
  .charts-section {
    grid-template-columns: 1fr;
  }
  .span-2 {
    grid-column: span 1;
  }
}

@media (max-width: 768px) {
  .home-container {
    padding: 16px;
  }
  
  .stats-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }
  
  .stat-card {
    padding: 20px;
  }
  
  .chart-container {
    height: 250px;
  }
}
</style>